<?php

/**
 * This is the model class for table "{{cpquotation}}".
 *
 * The followings are the available columns in table '{{cpquotation}}':
 * @property string $idcpquotation
 * @property string $idcptender
 * @property string $quotationdate
 * @property string $idcpsupplier
 * @property string $amt
 * @property string $tax
 * @property string $discount
 * @property string $total
 *
 * The followings are the available model relations:
 * @property Cporder[] $cporders
 * @property Cpsupplier $idcpsupplier0
 * @property Cptender $idcptender0
 * @property Cpquotationitem[] $cpquotationitems
 */
class Cpquotation extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Cpquotation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cpquotation}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idcptender, idcpsupplier, amt, tax, discount, total', 'required'),
			array('idcptender, idcpsupplier, amt', 'length', 'max'=>10),
			array('tax, discount, total', 'length', 'max'=>15),
			array('quotationdate', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idcpquotation, idcptender, quotationdate, idcpsupplier, amt, tax, discount, total', 'safe', 'on'=>'search'),
                        array('idcptender, idcpsupplier', 'validatefkeys'),
		);
	}        
        
        public function validatefkeys($attribute,$params){
               $aux = $attribute . '0';
               if(!$this->$aux)
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
        }      
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cporders' => array(self::HAS_MANY, 'Cporder', 'idcpquotation'),
			'idcpsupplier0' => array(self::BELONGS_TO, 'Cpsupplier', 'idcpsupplier'),
			'idcptender0' => array(self::BELONGS_TO, 'Cptender', 'idcptender'),
			'cpquotationitems' => array(self::HAS_MANY, 'Cpquotationitem', 'idcpquotation'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idcpquotation' => Yii::t('application', 'Idcpquotation'),
			'idcptender' => Yii::t('application', 'Idcptender'),
			'quotationdate' => Yii::t('application', 'Quotationdate'),
			'idcpsupplier' => Yii::t('application', 'Idcpsupplier'),
			'amt' => Yii::t('application', 'Amt'),
			'tax' => Yii::t('application', 'Tax'),
			'discount' => Yii::t('application', 'Discount'),
			'total' => Yii::t('application', 'Total'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idcpquotation',$this->idcpquotation,true);
		$criteria->compare('idcptender',$this->idcptender,true);
		$criteria->compare('quotationdate',$this->quotationdate,true);
		$criteria->compare('idcpsupplier',$this->idcpsupplier,true);
		$criteria->compare('amt',$this->amt,true);
		$criteria->compare('tax',$this->tax,true);
		$criteria->compare('discount',$this->discount,true);
		$criteria->compare('total',$this->total,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}